package com.kele.basic;

/**
 * @author chunhui.li
 * @date 2022/1/22 13:06
 * @Description
 * @ClassName AutmicTest
 */
public class AutmicTest {
    public static  int i = 0;

    public static void main(String[] args) throws InterruptedException {
        AutmicTest autmicTest = new AutmicTest();
        Thread t1 = new Thread(() -> autmicTest.add(1000000));
        Thread t2 = new Thread(() -> autmicTest.add(1000000));

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(i);
    }

    public  void add(int n) {
        synchronized (this){
            for (int m = 0; m < n; m++) {
                i++;
            }
        }

    }
}

